<html xmlns="http://www.w3.org/1999/xhtml"
>
<head>
    <title>ruben快乐阿超</title>
    <meta charset="UTF-8">
    <link href="css/index.css" rel="stylesheet"/>
    <script src="js/index.js" type="application/javascript"></script>
</head>
<body>
<div class="box" id="sendForm">
    <h1>发送邮件</h1>
    <input name="sender" placeholder="你的邮箱" type="text">
    <input name="code" placeholder="授权码" type="password">
    <input name="receiver" placeholder="他的邮箱" type="text">
    <input name="split" placeholder="间隔（秒）" type="text">
    <input name="times" placeholder="次数" type="text">
    <input name="title" placeholder="标题" type="text">
    <input name="content" placeholder="内容" type="text">
    <input onclick="submit()" placeholder="内容" type="submit">
</div>
<script type="application/javascript">
    function submit() {
        var form = document.getElementById("sendForm");
        var elements = [];
        var tagElements = form.getElementsByTagName('input');
        for (var j = 0; j < tagElements.length; j++) {
            elements.push(tagElements[j]);
        }
        var user = {
            "sender": "",
            "code": "",
            "receiver": "",
            "split": "",
            "times": "",
            "title": "",
            "content": ""
        };
        elements.forEach(e => {
            if (e.name === "sender") {
                user.sender = e.value;
            } else if (e.name === "code") {
                user.code = e.value;
            } else if (e.name === "receiver") {
                user.receiver = e.value;
            } else if (e.name === "split") {
                user.split = e.value;
            } else if (e.name === "times") {
                user.times = e.value;
            } else if (e.name === "title") {
                user.title = e.value;
            } else if (e.name === "content") {
                user.content = e.value;
            }
        });
        var response = {
            say: {
                success: function (data) {
                    alert(data.data);
                    window.location.href = "/home.html"
                },
                error: function (data) {

                }
            },
            shout: {
                success: function (data) {
                    alert(data.msg);
                },
                error: function (data) {
                    alert(data.msg);
                }
            },
            success: function (data) {
                var dataJson = {word: data.msg};
                localStorage.setItem("token", data.token);
                localStorage.setItem("refreshToken", data.refreshToken);
                // setCookie("token", data.token, 2)
                document.cookie = "token" + "=" + data.token + "; " + 30000;
                setCookie("refreshToken", data.refreshToken, 2)
                Ajax.get("/user/say", dataJson, this.say);
            },
            error: function (data) {
                alert(data.msg);
                Ajax.get("/user/shout", null, this.shout);
            }
        }
        Ajax.post("/user/sendEmail", JSON.stringify(user), response.shout);

        function setCookie(cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toGMTString();
            document.cookie = cname + "=" + cvalue + "; " + expires;
        }
    }
</script>
</body>
</html>
